<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="1"
      :labelWidth="100"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
      class="mtop"
    />
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary" :loading="saveLoading">保 存</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { insertStationMaintaion, updataStationMaintaion } from '@/api/cms-api/personnelInfo/postSet';
export default {
  props: {
    value: {
      type: Object,
      default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      formData: [],
      stationId: '',
      initForm: this.initFormFun(),
      saveLoading: false
    };
  },
  mounted() {
    if (this.value != null && this.value.type == 'edit') {
      this.init();
    }
  },
  methods: {
    //初始化数据
    init() {
      this.stationId = this.value.rowData.stationId;
      //循环为表单赋值
      this.initForm.forEach(x => {
        x.initialValue = this.value.rowData[x.fieldName];
      });
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '岗位代码',
          fieldName: 'stationCode',
          //placeholder: '应和培训系统的相应岗位保持一致',
          initialValue: '',
          //disabled:true,
          disabled:this.value != null && this.value.type == 'edit',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },
            {
              max: 60,
              message: '长度最大60个字符',
              trigger: 'blur'
            }
            ,
            {
              pattern:/^[a-zA-Z0-9_]{0,}$/,
              message: '不能输入汉字和特殊符号',
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'INPUT',
          label: '岗位名称',
          fieldName: 'stationName',
          placeholder: '请输入',
          initialValue: '',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            },

            {
              max: 300,
              message: '长度最大300个字符',
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '是否培训岗位',
          fieldName: 'isTrain',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1004'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '启用标志',
          fieldName: 'enable',
          placeholder: '',
          initialValue: 10031001,
          itemList: this.createDictList('1003'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '岗位类型',
          fieldName: 'businessType',
          placeholder: '',
          initialValue: '',
          itemList: this.createDictList('1626'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },

        // {
        //   type: 'SELECT',
        //   label: '关键提醒',
        //   fieldName: 'keyNotification',
        //   placeholder: '请选择',
        //   initialValue: '',
        //   itemList: this.createDictList('1004'),
        //   placeholder: '请输入'
        // },
        {
          type: 'INPUT',
          label: '岗位描述',
          fieldName: 'stationDesc',
          rows: 3,
          initialValue: '',
          placeholder: '请输入',
          rules: [
            {
              max: 1000,
              message: '长度最大1000个字符',
              trigger: 'blur'
            }
          ]
        }
      ];
    },
    //保存校验 async
    async formChangeHandle(val) {
      this.formData = val;
      this.saveLoading = true;
      let res;
      //如果传入参数不为空并且为修改
      if (this.value != null && this.value.type == 'edit') {
        res = await updataStationMaintaion(this.formData, this.stationId);
      } else {
        res = await insertStationMaintaion(this.formData);
      }
      if (res.resultCode === 200) {
        this.saveLoading = false;
        this.$notify.success({ title: '提示信息', message: '保存成功!' });
        // 重新加载
        this.closeDialog('refresh');
      } else {
        this.saveLoading = false;
        this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
        // this.closeDialog();
      }
    },
    // //保存   设置点击后 保存按钮变黑
    saveDialog(val) {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', { visible: false, type: type });
    }
  }
};
</script>
